package com.superboo.core.controller;

import com.superboo.base.properties.Main;
import com.superboo.core.domain.Girl;
import com.superboo.core.domain.Result;
import com.superboo.core.service.GirlService;
import com.superboo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * GirlController
 *
 * @author peiran zhang
 * @date 2017/3/20
 */
@RestController
public class GirlController {

    @Autowired
    private GirlService girlService;

    @Autowired
    private Main main;

    @GetMapping(value="/girls")
    public List<Girl> list(){
        System.out.println(main.getName());
        return girlService.findAll();
    }

    /**
     * 添加一个女生
     *
     * @param girl
     * @return
     */
    @PostMapping(value = "/girls")
    public Result<Girl> add(@Valid Girl girl, BindingResult bindingResult) {

        //验证是否有错误
        if (bindingResult.hasErrors()) {
            return ResultUtil.validationError(bindingResult.getFieldError().getDefaultMessage());
        }

        girl.setCupSize(girl.getCupSize());
        girl.setAge(girl.getAge());

        return ResultUtil.success(girlService.save(girl));

    }

    /**
     * 根据id获得年龄
     * @param id
     */
    @GetMapping(value = "/girls/age/{id}")
    public void getAge(@PathVariable("id") Long id) throws Exception{
        girlService.getAge(id);
    }
}
